﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataProvider;
using System.Data.SqlClient;
using System.Collections;
using System.Data;
namespace DalatCooking.Core
{
    public class Cart : Course
    {
        public int CartID { get; set; }
        public int Quantity { get; set; }
        public float SubTotal { get { return Quantity * Price; } }
        public bool Compare(int courseId)
        {
            return this.CourseId == courseId;
        }

       List<Cart> list;
        public List<Cart> ShoppingCart { get { return list; } }
        public Cart()
        {
            list = new List<Cart>();
        }
        public int SearchInCart(int itemId)
        {
            int index = 0;
            foreach (Cart item in list)
            {
                if (list[index].Compare(itemId))
                {
                    return index;
                }
                index++;
            }
            return -1;
        }
        public void AddToCart(Cart entity)
        {
            int index = SearchInCart(entity.CourseId);
            if (index >= 0)
                list[index].Quantity++;
            else list.Add(entity);
        }
        public void RemoveCart(int courseId)
        {
            int index = SearchInCart(courseId);
            if (index >= 0 && index < list.Count)
                list.RemoveAt(index);
        }
        public void UpdateCart(int courseId, int quantity)
        {
            int index = SearchInCart(courseId);
            if (index >= 0 && index < list.Count)
                list[index].Quantity = quantity;
        }
        public float Totals()
        {
            float total = 0;
            for (int i = 0; i < list.Count; i++)
                total += list[i].SubTotal;
            return total;
        }
        public int CountQuantity()
        {
            int quantity = 0;
            for (int i = 0; i < list.Count; i++)
                quantity = list[i].Quantity;
            return quantity;
        }

        public void Checkout(Orders entity)
        {
            int orderID = OrdersManager.Insert(entity);
            if (orderID > 0)
            {
            // AddMultiRows
                
                AddMultiRows rows = new AddMultiRows();

                string SPName = "OrderDetail_Insert";
                SqlParameter[] paras = new SqlParameter[4];
                paras[0] = new SqlParameter("@OrderID", SqlDbType.Int);
                paras[1] = new SqlParameter("@CourseID", SqlDbType.Int);
                paras[2] = new SqlParameter("@Quantity", SqlDbType.Int);
                paras[3] = new SqlParameter("@Price", SqlDbType.Int);

                rows.Begin(SPName, paras, CommandType.StoredProcedure);

                //  Chen gio hang vao - duyet list
                
                ArrayList listValue = new ArrayList();
                for (int i = 0; i < list.Count; i++)
                {
                    listValue.Clear();
                    listValue.Add(orderID);
                    listValue.Add(list[i].CourseId);
                    listValue.Add(list[i].Quantity);
                    listValue.Add(list[i].Price);

                    rows.Executing(listValue);
                }
                rows.Completed();
            }
        }
    }
}
